// pages/wsh/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        recruit:[],  
    },
    getRecruitByRecruitState:function(recruitState){
        return new Promise((reslove, reject) => {
            wx.cloud.callFunction({
                name:'recruit',
                data:{
                    parameter:'getRecruitByRecruitState',
                    recruitState:recruitState,
                },
                success: (res) => {
                reslove(res.result);
                console.log('获取招募信息成功',res)
                },
                fail: (msg) => {
                reject('请求失败');
                }
            })
        }) 
    },

    toDetailsTap:function(e){
        console.log(e)
        const id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '/pages/details/details?id='+id,
        })
    },
    // 根据id修改招募状态
    updateRecruitStateById:function(recruitState,recruitId){
        wx.showLoading({
          title: '',
        })
        wx.cloud.callFunction({
            name:'recruit',
            data:{
                parameter:'updateRecruitStateById',
                id:recruitId,
                recruitState:recruitState,
            }
        }).then(res=>{
            console.log('修改成功',res)
            wx.hideLoading()
        }).catch(err=>{
            console.log('修改失败',err)
            wx.hideLoading()
        })
    },
    // 审核
    sh:function(e){
        console.log(e.currentTarget.dataset)
        const id = e.currentTarget.dataset.id
        const recruitId = e.currentTarget.dataset.recruitid
        const index = e.currentTarget.dataset.i
        console.log(recruitId)
        if(id=='1'){ //通过
            console.log('通过审核')
            this.updateRecruitStateById(1,recruitId)
        }else{
            this.updateRecruitStateById(2,recruitId)
        }
        this.data.recruit.splice(index, 1); 
        this.setData({
            recruit: this.data.recruit
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getRecruitByRecruitState(0).then(res=>{
            console.log(res)
            this.setData({
                recruit:res
            })
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})